package com.wenhua.junshao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenhua.junshao.common.R;
import com.wenhua.junshao.dot.DishDto;
import com.wenhua.junshao.entity.Category;
import com.wenhua.junshao.entity.Dish;
import com.wenhua.junshao.entity.DishFlavor;
import com.wenhua.junshao.service.CategoryService;
import com.wenhua.junshao.service.DishFlavorService;
import com.wenhua.junshao.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;


    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品信息分页查询
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {




        //创建分页构造器
        Page<Dish> pageInfo1 = new Page(page, pageSize);
        //添加条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByAsc(Dish::getName);
        dishService.page(pageInfo1, queryWrapper);


        //创建另一个page对象
        Page<DishDto> pageInfo2 = new Page<>();
        //拷贝除records以外的其他属性
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");

        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : pageInfo1.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //增加分类名称,dish表中和category表中菜品category_id一样
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String CategoryName = category.getName();
            dishDto.setCategoryName(CategoryName);
            dishDtoList.add(dishDto);
        }
        pageInfo2.setRecords(dishDtoList);
        return R.success(pageInfo2);
    }

    /**
     * 依据id查询菜品信息和口味信息并回显
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);

    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }


    /**
     * 根据条件查询菜品列表
     */

    @GetMapping("list")
    @Cacheable(cacheNames ="dishCache",key = "#dish.categoryId+'_'+#dish.name+'_'+#dish.status" )
    public R<List<DishDto>> list(Dish dish) {
//       //构建动态key
//        String key = "dish-"+dish.getCategoryId();
//        List<DishDto> redis = (List<DishDto>) redisTemplate.opsForValue().get(key);
//        if (redis != null) {
//             return  R.success(redis);
//         }
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish::getStatus, 1);
        queryWrapper1.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
      queryWrapper1.eq(StringUtils.isNotBlank(dish.getName()), Dish::getName, dish.getName());
        queryWrapper1.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        //查询列表
        List<Dish> dishList = dishService.list(queryWrapper1);

        //将每个Dish对象转换为DishDto
        List<DishDto> dishDtoList = dishList.stream().map(item -> {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            LambdaQueryWrapper<DishFlavor> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
//        redisTemplate.opsForValue().set(key,dishDtoList);
        return R.success(dishDtoList);
    }
}


//    @GetMapping("page")
//    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
//        //构造分页构造器对象
//        Page<Dish> pageInfo1 = new Page<>(page, pageSize);
//        //条件构造器
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        //添加过滤条件
//        queryWrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);
//        //添加排序条件
//        queryWrapper.orderByDesc(Dish::getUpdateTime);
//        //执行分页查询
//        dishService.page(pageInfo1, queryWrapper);
//
//        //对象拷贝
//        Page<DishDto> pageInfo2 = new Page<>(page, pageSize);
//        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
////
////        List<DishDto> dishDtoList = new ArrayList<>();
////        for (Dish dish : pageInfo1.getRecords()) {
////            DishDto dishDto = new DishDto();
////            BeanUtils.copyProperties(dish, dishDto);
////            //根据id查询分类对象
////            Category category = categoryService.getById(dish.getCategoryId());
////            //设置分类名称
////            dishDto.setCategoryName(category.getName());
////            dishDtoList.add(dishDto);
////        }
//
//
//        List<DishDto> dishDtoList = pageInfo1.getRecords().stream().map(dish -> {
//            DishDto dishDto = new DishDto();
//            BeanUtils.copyProperties(dish, dishDto);
//            //根据id查询分类对象
//            Category category = categoryService.getById(dish.getCategoryId());
//            //设置分类名称
//            dishDto.setCategoryName(category.getName());
//            return dishDto;
//
//        }).collect(Collectors.toList());
//
//        pageInfo2.setRecords(dishDtoList);
//
//        return R.success(pageInfo2);
//    }
